#include "socket_address.h"

#include <cstdio>
#include <gtest/gtest.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>

TEST(SocketAddress, Create)
{
    SocketAddress address = SocketAddress::Address("127.0.0.1", 10000);
    printf("%s:%d.\n", address.AddressToString().c_str(), address.PortToNumber());
    EXPECT_STREQ("127.0.0.1", address.AddressToString().c_str());
    EXPECT_EQ(10000, address.PortToNumber());
}

TEST(SocketAddress, BindTest)
{
    SocketAddress address     = SocketAddress::Address("127.0.0.1", 10000);
    auto          sai         = address.NetAddress();
    int           description = socket(sai.sin_family, SOCK_STREAM, 0);
    int           result      = bind(description, (struct sockaddr *)&sai, sizeof(sai));
    printf("bind result : %d.\n", result);
    close(description);
    EXPECT_EQ(result, 0);
}

TEST(GetAddressInfo, Domain)
{
    std::string                domain   = "www.baidu.com";
    std::string                protocol = "http";
    std::vector<SocketAddress> result_vec;
    int                        result = SocketAddress::Address(domain, protocol, result_vec);
    EXPECT_EQ(1, result);
    for (auto & data : result_vec)
    {
        printf("%s:%d.\n", data.AddressToString().c_str(), data.PortToNumber());
    }
    result_vec.clear();
    result = SocketAddress::Address(domain, 443, result_vec);
    EXPECT_EQ(1, result);
    for (auto & data : result_vec)
    {
        printf("%s:%d.\n", data.AddressToString().c_str(), data.PortToNumber());
    }
}
